<template>
  <el-dialog  title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <!-- 分配角色 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.rolesId" :label="item.rolesId">
        {{
          item.name
        }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {getrolesList,getdetailRoles,setUsersRoles} from "@/api/roles";

export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      list: [], // 角色列表
      roleIds: [], //
    }
  },
  created() {
    this.getrolesList()
  },
  methods:{
    //获取角色列表
    async getrolesList(){

     const { row}= await  getrolesList()
      this.list=row

    },
    //根据用户id获取用户的角色
  async  getdetailRoles(id){
      //因为prop的传值时异步的，所以不能直接this.userId
    const result=await getdetailRoles(id)
    console.log(result)
    for (let item in result){
      this.roleIds.push(result[item].roles_id)

      }
    },
    //关闭窗口
    btnCancel(){
      this.roleIds=[]
      this.$emit('update:showRoleDialog', false) // 关闭



    },
    //点击确认
    async btnOk(){

      console.log({userId: this.userId,roleIds:this.roleIds})
      await  setUsersRoles({userId: this.userId,roleIds:this.roleIds})
      this.$message.success('更新角色成功')
      this.$emit('update:showRoleDialog',false)


    }


  }



}
</script>

<style scoped>

</style>
